#!/bin/bash

sudo rm /tmp/tstick

wget http://telecom.co.nz/mbbmeter -O /tmp/tstick >/dev/null 2>&1

#BALANCE=`fgrep "$" tmp | awk '{sub(/^[ \t]+/, ""); print}'`
#MB_USED=`fgrep "usageBarValuePositionRight" tmp -A1 | awk '{ field = $(NF-1) }; END{ print field }'`

#balance=`fgrep "current balance" /tmp/tstick | awk '{print $NF}' | nawk 'sub(".....$", "")'`
balance=`cat /tmp/tstick | awk '/Your current balance is / {print $7}' | awk '{sub(/<\/h2>/,""); print}'`

echo $balance
# mb_remain=`fgrep "remaining data" /tmp/tstick | awk '{print $(NF-1) }'`

string=$balance" remaining on "`date`
# string=$mb_remain" MB of data remaining on "`date`

# strip the dollar sign to get just a number
dollars=`echo $balance | sed 's/\\$//g'`
low=`echo "$dollars < 10.00" | bc`

if [ "$low" -eq 1 ]; then
 sendEmail -f ryanscreek@gmail.com -t tom88holmes@gmail.com -u "Weather station TStick balance low!" -m $string
fi
 
if [ -n "$balance" ]
then
	echo $string >> /home/pi/logs/tstick_usage_log
	exit 0
else
	echo "Didn't work!"
	exit 1
fi

exit 0

